package com.News360.createaccounttest;

import java.io.FileReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import static org.junit.Assert.*;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.*;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;

import com.News360.action.singin.*;
import com.opera.core.systems.OperaDriver;

public class CreateAccountBase {

	protected WebDriver driver;
	protected String baseUrl /*= "http://news360.com"*/;
	protected StringBuffer verificationErrors = new StringBuffer();
	protected ElementAction elementAction;
	

	@BeforeClass
	@Parameters({"configFile"})
	public void setUp(@Optional String configFile) throws Exception {
		if (configFile == null ) {
			configFile = System.getProperty("configFile");
		}
		if (configFile == null ) {
			configFile = System.getenv("configFile");
		}
		if (configFile == null ) {
			configFile = "app.properties";
		}
		Properties props = new Properties();
		props.load(new FileReader(configFile));
		
		baseUrl = props.getProperty("baseUrl", "http://news360.com");
		
		String remote = props.getProperty("remote");
		String browser = props.getProperty("browser", "firefox");
		
		if (remote == null ) {
			DesiredCapabilities caps;
			if ("firefox".equals(browser)) {
				driver = new FirefoxDriver();
			} else if ("ie".equals(browser)) {
				caps = DesiredCapabilities.internetExplorer();
				driver = new InternetExplorerDriver();
			} else if ("chrome".equals(browser)) {
				caps = DesiredCapabilities.chrome();
				System.setProperty("webdriver.chrome.driver","C:\\apps\\chromedriver.exe");
				driver = new ChromeDriver(caps);
			} else if ("opera".equals(browser)) {
				driver = new OperaDriver();
			} else {
				driver = new HtmlUnitDriver();
			}
			
		} else {
			DesiredCapabilities remcaps;
			if ("ie".equals(browser)) {
				remcaps = DesiredCapabilities.internetExplorer();
			} else {
				remcaps = new DesiredCapabilities();
				remcaps.setBrowserName(browser);
			}
			try {
				driver = new RemoteWebDriver(new URL(remote), remcaps);
			} catch (MalformedURLException e) {
				throw new IllegalArgumentException(e);
			}
		}
		
		elementAction = new ElementAction(driver);
		String ImpWait = props.getProperty("implicitWait", "15");
		driver.manage().timeouts().implicitlyWait(Integer.parseInt(ImpWait), TimeUnit.SECONDS);
	}

	@AfterClass
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
	
	/*/@AfterClass
	public void logOut() throws Exception {
		SignOut signOut = new SignOut(driver);
		signOut.signOut();
		System.out.println("vyshel");
	}*/
	
	protected boolean isElementPresent(By by) {
		return elementAction.isElementPresent(by);
	}
	
	protected boolean hasElementText(By by) {
		return elementAction.hasElementText(by);
	}
	
	protected boolean waitForElement(By by) {
		return elementAction.waitForElement(by);
	}
	
	protected void log (Object message) {
		elementAction.log(message);
	}

	protected void gotoBaseURL() {
		driver.get(baseUrl);
	}

	protected void clickCreateAccountButton() {		
		driver.findElement(By.xpath("//div[@class='clustersList']/div[19]"));
		WebElement element = driver.findElement(By.cssSelector(".btn_createNewAccount"));
		Actions builder = new Actions(driver);
		builder.moveToElement(element).click().perform();		
	}
	
	protected void clickCreateAccountButton2() {		
		driver.findElement(By.xpath("//div[@class='step2']/a")).click();		
	}
	
	protected void assertCreateEmailDialog() {	
		List<WebElement> allSuggestions = driver.findElements(By.cssSelector(".popupWinTitle"));
        assertEquals("Create new account", allSuggestions.get(1).getText());
        List<WebElement> allSuggestions2 = driver.findElements(By.cssSelector(".popupWinBody > div > p"));
		assertTrue(allSuggestions2.get(2).getText().startsWith("Please enter a"));
		assertTrue(isElementPresent(By.cssSelector("span.btn_createAccount")));
		assertEquals("Already have account?", driver.findElement(By.cssSelector(".box_foot_popup > p")).getText());
		assertTrue(isElementPresent(By.cssSelector("span.btn_signIn")));
		assertTrue(isElementPresent(By.cssSelector("div.popupWinClose")));	
	}
	
	protected void clickRegister() {
		driver.findElement(By.cssSelector("span.btn_createAccount")).click();
	}
	
	protected void inputLoginAndPassSignInEmail(String login, String pass) {
		SignInEmailAction logon = new SignInEmailAction(driver);
		logon.signInEmail(login, pass);
	}

	protected void clickConnectWithEmail() {
		driver.findElement(By.linkText("Connect with Email")).click();
	}

	protected void clickSignInEmailDialog() {
		driver.findElement(By.cssSelector("div.box_sign-in-email > span.btn_signIn")).click();
	}

	protected void clickConnectWithGoogleID() {
		driver.findElement(By.linkText("Connect with Google Id")).click();
	}
	
	protected void signInGoogle(String login, String pass) {
		SignInGoogleAction logon = new SignInGoogleAction(driver);
		logon.signInGoogle(login, pass);
	}
	
	protected void clickConnectWithFacebook() {
		driver.findElement(By.linkText("Connect with Facebook")).click();
	}
	
	protected void signInFacebook(String login, String pass) {
		SignInFacebookAction logon = new SignInFacebookAction(driver);
		logon.signInFacebook(login, pass);
	}
	
	protected void clickConnectWithTwitter() {
		driver.findElement(By.linkText("Connect with Twitter")).click();
	}
	
	protected void signInTwitter(String login, String pass) {
		SignInTwitterAction logon = new SignInTwitterAction(driver);
		logon.signInTwitter(login, pass);
	}
	
	protected void verifyMyProfileLinkAvailable() {
		driver.findElement(By.cssSelector(".btn_myProFile"));
	}

	protected void assertCreateAccountDialog() {
		waitForElement(By.cssSelector(".btn_createNewAccount"));
		Assert.assertTrue(isElementPresent((By.cssSelector(".popupWinClose"))));
		Assert.assertEquals("Create new account", driver.findElement(By.cssSelector("h1.popupWinTitle")).getText());
		Assert.assertTrue(isElementPresent((By.xpath("//div[@class='popupWinBody']/div/p/img"))));
		Assert.assertTrue(driver.findElement(By.xpath("//div[@class='popupWinBody']/div/p[2]")).getText().startsWith("It will allow you to"));
		Assert.assertEquals("Sign in with", driver.findElement(By.xpath("//div[@class='step1']/p")).getText());
		Assert.assertTrue(isElementPresent((By.linkText("Connect with Facebook"))));
		Assert.assertTrue(isElementPresent((By.linkText("Connect with Twitter"))));
		Assert.assertTrue(isElementPresent((By.linkText("Connect with Google Id"))));
		Assert.assertEquals("Don`t have any of these? Use your e-mail", driver.findElement(By.xpath("//div[@class='step1']/p[2]")).getText());
		Assert.assertTrue(isElementPresent((By.linkText("Connect with Email"))));
	}

	protected void assertCreateAccountDialog2() {
		waitForElement(By.xpath("//span[contains(.,'http://news360.com/')]"));
		Assert.assertTrue(isElementPresent((By.cssSelector(".popupWinClose"))));
		Assert.assertEquals("Create new account", driver.findElement(By.cssSelector("h1.popupWinTitle")).getText());
		Assert.assertTrue(isElementPresent((By.xpath("//div[@class='popupWinBody']/div/p/img"))));
		Assert.assertTrue(driver.findElement(By.xpath("//div[@class='popupWinBody']/div/p[2]")).getText().startsWith("It will allow you to"));
		Assert.assertEquals("You can also customize the web the web address of your profile:", driver.findElement(By.xpath("//div[@class='step2']/p")).getText());
		Assert.assertTrue(isElementPresent((By.xpath("//div[@class='url_box']/div/input"))));
		WebElement element = driver.findElement(By.xpath("//div[@class='url_box']/div/input"));
		log(element.getText());
		hasElementText(By.xpath("//div[@class='url_box']/div/input[@value]"));
		Assert.assertTrue(isElementPresent(By.xpath("//div[@class='url_box']/div/input")));
		Assert.assertTrue(isElementPresent((By.linkText("Create account"))));
	}

	

}

